package com.babyqiao.wallpaper.manager.service.impl;

import com.babyqiao.wallpaper.manager.controller.dto.AddCategoryParams;
import com.babyqiao.wallpaper.manager.dao.CategoryMapper;
import com.babyqiao.wallpaper.manager.entity.WallpaperCategory;
import com.babyqiao.wallpaper.manager.entity.vo.CategoryVO;
import com.babyqiao.wallpaper.manager.service.CateagryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.babyqiao.wallpaper.manager.utils.common.GlobalConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类管理service 实现类
 *
 * @author qinglongzhai
 * @date 2020/12/12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, WallpaperCategory> implements CateagryService {


    /**
     * 查询是否存在分类
     *
     * @param title    类别名称
     * @param level    类别的级别
     * @param parentId 父类ID
     * @return 是否存在
     */
    @Override
    public boolean queryOneCategory(String title, String level, String parentId) {
        QueryWrapper<WallpaperCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title);
        queryWrapper.eq("level", level);
        queryWrapper.eq("parent_id", parentId);
        int count = this.count(queryWrapper);
        return count <= 0;
    }


    /**
     * 添加分类
     *
     * @param addCategoryParams 添加分类参数
     * @return 是否添加成功
     */
    @Override
    public boolean saveCategory(AddCategoryParams addCategoryParams) {

        WallpaperCategory wallpaperCategory = new WallpaperCategory();
        boolean b = queryOneCategory(addCategoryParams.getTitle(), addCategoryParams.getLevel(), addCategoryParams.getParentId());
        if (b) {
            BeanUtils.copyProperties(addCategoryParams, wallpaperCategory);
            return save(wallpaperCategory);

        }
        return false;
    }

//    /**
//     * 添加查询分类条件
//     *
//     * @param addCategoryParams 条件
//     * @return
//     */
//    @Override
//    public QueryWrapper<WallpaperCategory> addSelectCondition(AddCategoryParams addCategoryParams) {
//        QueryWrapper<WallpaperCategory> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("level",addCategoryParams.getLevel());
//        queryWrapper.eq("parent_id",addCategoryParams.getParentId());
//
//
//        return queryWrapper;
//    }

    /**
     * 查询全部分类并整理
     *
     * @return
     */
    @Override
    public List<CategoryVO> getAllcategory() {

        CategoryVO categoryVO;
        //顶级分类
        List<CategoryVO> top = new ArrayList<>();
        //一级分类
        List<CategoryVO> one = new ArrayList<>();
        //二级分类
        List<CategoryVO> two = new ArrayList<>();
        //查询所有分类
        List<WallpaperCategory> listCategory = this.list();
        //根据level字段,区分一二级分类并放入对应的List中
        for (WallpaperCategory category : listCategory) {
            if (GlobalConstant.CATEGORY_LEVEL_ONE.getLevel().equals(category.getLevel())) {
                categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                one.add(categoryVO);
            } else if (GlobalConstant.CATEGORY_LEVEL_TWO.getLevel().equals(category.getLevel())) {
                categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                two.add(categoryVO);
            }

        }
        //将二级分类整理至一级分类
        one.forEach(oneItem -> {
            List<CategoryVO> finallyCategory = new ArrayList<>();
            two.forEach(twoItem -> {
                if (oneItem.getId().equals(twoItem.getParentId())) {
                    finallyCategory.add(twoItem);
                }
            });
            oneItem.setChildren(finallyCategory);
        });
        //设置顶级分类
        categoryVO = new CategoryVO();
        categoryVO.setId("0");
        categoryVO.setLevel("0");
        categoryVO.setTitle("分类");
        categoryVO.setChildren(one);
        top.add(categoryVO);
        return top;
    }
}
